package com.blb.service.impl;

import com.blb.mapper.CooperativePartnerDao;
import com.blb.mapper.IndustryManagementDao;
import com.blb.pojo.BranchCompany;
import com.blb.pojo.CooperativePartner;
import com.blb.pojo.IndustryManagement;
import com.blb.pojo.PageEntity;
import com.blb.service.CooperativePartnerService;
import com.blb.util.Page;
import com.blb.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author hxf
 */
@Service
public class cooperativePartnerServiceImpl implements CooperativePartnerService {

    @Autowired
    private CooperativePartnerDao cooperativePartnerDao;

    @Autowired
    private IndustryManagementDao industryManagementDao;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * @return
     * @author :hxf
     * 查询所有IndustryManagement对象，sql语句做了限制，防止前端图片获取过多影响布局，之战是前8个
     */
    @Override
    public List<IndustryManagement> selectIndustryIcon() {
        String key = "IndMAll";
        ValueOperations<String, List<IndustryManagement>> operations = redisTemplate.opsForValue();
        //判断redis中是否有键为key的缓存
        Boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
            System.out.println("缓存获取数据");
            return operations.get(key);
        } else {
            List<IndustryManagement> industryManagements = cooperativePartnerDao.selectIndustryIcon();
            System.out.println("数据库获取数据 ");
            //写入缓存
            operations.set(key, industryManagements, 1, TimeUnit.MINUTES);
            return industryManagements;
        }

    }

    /**
     * @param id Industry_id
     * @return
     * @author :hxf
     * 合作伙伴模块,根据Industry_id可以获取 行业 和所有公司logo和伙伴名称
     */
    @Override
    public Map selectCPAndIM(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        String key = "coopP_" + id;
        ValueOperations<String, Map> operations = redisTemplate.opsForValue();
        //判断redis中是否有键为key的缓存
        Boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
            System.out.println("缓存获取数据");
            return operations.get(key);
        } else {
            //根据Industry_id获取所有公司logo和伙伴名称
            List<CooperativePartner> cooperativePartners = cooperativePartnerDao.selectCP(id);
            //根据Industry_id可以获取行业
            IndustryManagement industryManagement = industryManagementDao.selectByPrimaryKey(id);
            //根据行业图标获取Industry_id
            map.put("cooperativePartners", cooperativePartners);
            map.put("industryManagement", industryManagement);
            //写入缓存
            operations.set(key, map, 1, TimeUnit.MINUTES);
            return map;
        }
    }

    /**
     * @param nowPage  当前页
     * @param pageSize 每页展示的条数
     * @return
     * @author :hxf
     * 分页
     */
    @Override
    public Page selectAllLoge(Integer nowPage, Integer pageSize) {
        String key = "coopPS_" + nowPage + "" + pageSize;
        ValueOperations<String, Page> operations = redisTemplate.opsForValue();
        //判断redis中是否有键为key的缓存
        Boolean hasKey = redisTemplate.hasKey(key);
        if (hasKey) {
            System.out.println("缓存获取数据");
            return operations.get(key);
        } else {
            System.out.println(" 数据库读取数据");
            //分页
            List<CooperativePartner> cooperativePartners = cooperativePartnerDao.selectAllLogo((nowPage - 1) * pageSize, pageSize);
            List<CooperativePartner> cooperativePartners1 = cooperativePartnerDao.selectAllLogo(0, 0);
            Page page = PageUtil.page(cooperativePartners, cooperativePartners1, nowPage, pageSize);
            //写入缓存
            operations.set(key, page, 1, TimeUnit.MINUTES);
            return page;
        }
    }

}
